package com.atguigu.petstore.sys.controller;

import com.atguigu.petstore.common.result.Result;
import com.atguigu.petstore.sys.service.SysLoginService;
import com.atguigu.petstore.sys.service.impl.SysCaptchaService;
import com.atguigu.petstore.sys.vo.SysLoginResp;
import com.atguigu.petstore.sys.vo.SysLoginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Tag(name="登录页")
@RequestMapping("api/sys")
@RestController
public class SysLoginController {
    @Autowired
    SysLoginService sysLoginService;
    @Autowired
    SysCaptchaService sysCaptchaService;
//验证码也需要返给前端所以在controller层写
@GetMapping("/captcha")
public void  getCaptcha(HttpServletResponse response,
                        @RequestParam("deviceId") String deviceId) throws IOException {


    BufferedImage bufferedImage = sysCaptchaService.generateCaptcha(deviceId);


    //告诉浏览器这个是图片
    response.setContentType("image/jpeg");
    //写给浏览器
    ImageIO.write(bufferedImage,"jpeg",response.getOutputStream());

}
    @Operation(summary = "系统管理用户登录")
    @PostMapping("user/login")
    public Result Login(@RequestBody SysLoginVo sysLoginVo){
        String token = sysLoginService.login(sysLoginVo);
        return Result.ok(token);
    }


}
